package com.cheng.reggie.mapper;

import com.cheng.reggie.dto.DishDto;
import com.cheng.reggie.entity.Category;
import com.cheng.reggie.entity.Dish;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface DishMapper {
    //根据类别id查询菜品数量
    @Select("SELECT COUNT(*) FROM dish where category_id=#{categoryId} and is_deleted=0")
    Integer countByCid(Long categoryId);

    //插入菜品数据
    @Insert("insert  into dish values(null,#{name},#{categoryId},#{price},#{code},#{image},#{description},#{status},#{sort}," +
            "#{createTime},#{updateTime},#{createUser},#{updateUser},0)")
    //默认情况,数据库表中这条数据有主键,但是dishDto的属性id是null
    //目标:执行插入后将数据库生成的id数据返回设置给dishDto对象id属性
    //useGeneratedKeys = true 获取生成的主键
    //keyColumn = "id" ,keyProperty = "id" 将插入后将数据库生成的id数据返回设置给dishDto对象id属性
    @Options(useGeneratedKeys = true,keyColumn = "id" ,keyProperty = "id")
    Integer insert(DishDto dishDto);

    //分页查询(方式:分表查) : 第一条sql,按照菜品分页查询
    List<Dish> findAll(@Param("name") String name);

    //分页查询(连表查):多表连接查询
    List<DishDto> findAll2(@Param("name") String name);

    //通过菜品id查询一个菜品(一个菜品有多个口味)
    DishDto findById(Long id);

    //根据id修改菜品
    Integer updateById(DishDto dishDto);

    //根据分类id去查询菜品列表方法
    @Select("select  * from dish where category_id=#{categoryId} and is_deleted=0 order by sort asc ")
    List<Dish> findByCategoryId(Long categoryId);

    //根据类别id和状态去查询菜品列表
    @Select("select  * from dish where category_id=#{categoryId} and is_deleted=0 order by sort asc ")
    List<Dish> findByCategoryIdAndStatus(@Param("categoryId") Long categoryId,
                                         @Param("status") Integer status);
}
